//
//  CommodityDetailsVC.m
//  phmall
//
//  Created by admin on 2017/9/17.
//  Copyright © 2017年 admin. All rights reserved.
//

#import "IntegralDetailsVC.h"
#import "ShopInfoModel.h"
#import "GoodsNameView.h"
#import "StoreInfoView.h"
#import "EvaluateView.h"
#import "Orderheader.h"
#import "GoodsQRCode.h"
#import "RecommendView.h"
#import "ActionView.h"
@interface IntegralDetailsVC ()<UITableViewDelegate,UITableViewDataSource,SDCycleScrollViewDelegate,UIWebViewDelegate,UIScrollViewDelegate>
{
    UIView *headerView;
    //    UILabel *drag;
    StoreInfoView *store;
    EvaluateView *evaluate;
    Orderheader *_headView;
    CGFloat height;
    GoodsNameView *name;
    NSString    *alpha;
}
@property(strong,nonatomic)UITableView *myTableview;
@property(nonatomic,strong)SDCycleScrollView *scrollView;
@property(nonatomic,strong)UIScrollView *scroll;
@property(nonatomic,strong)UIWebView *webView;
//轮播图数组
@property(nonatomic,strong)NSMutableArray *cycleArry;
//商店信息
@property(nonatomic,strong)ShopInfoModel *shopInfo;
@end

@implementation IntegralDetailsVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    //隐藏标签栏
    self.navBarBgAlpha=alpha;
    self.navigationController.navigationBar.barTintColor = [UIColor whiteColor];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navBarBgAlpha=alpha=@"0.0";
    UIBarButtonItem* leftBar=[[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"返回"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStyleDone target:self action:@selector(leftBarAction)];
    
    self.navigationItem.leftBarButtonItem=leftBar;
    //表格
    [self.view addSubview:self.myTableview];
    [self.myTableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view).offset(-STATUS_BAR_HEIGHT-44);
        make.left.right.equalTo(self.view);
        make.bottom.equalTo(self.view).with.offset(-(44+self.tabBar_height));
    }];
    
    [self getHeaderView];
    //底部按钮
    [self getFooterView];
    
    // Do any additional setup after loading the view.
}
-(void)getHeaderView{
    //整体界面
    headerView=[UIView new];
    headerView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    //轮播图
    self.scrollView = [[SDCycleScrollView alloc] init];
    self.scrollView.autoScrollTimeInterval=5;
    self.scrollView.pageControlDotSize=CGSizeMake(6, 6);
    self.scrollView.pageDotColor=[UIColor blackColor];
    self.scrollView.currentPageDotColor=[UIColor orangeColor];
    [headerView addSubview:self.scrollView];
    self.scrollView.delegate=self;
    [_scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headerView).priorityHigh();
        make.left.equalTo(headerView).priorityHigh();
        make.right.equalTo(headerView).priorityHigh();
        make.height.mas_equalTo(SCREEN_WIDTH).priorityHigh();
    }];
    _scrollView.bannerImageViewContentMode=UIViewContentModeScaleToFill;
    NSMutableArray *cycle=[[NSMutableArray alloc] init];
    for (cycleModel *model in self.cycleArry) {
        [cycle addObject:[NSString stringWithFormat:@"%@%@",IMGURL,model.img]];
    }
    _scrollView.imageURLStringsGroup=cycle;
    
    
    //商品名称视图
    name=[[GoodsNameView alloc] init];
    [headerView addSubview:name];
    [name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scrollView.mas_bottom);
        make.left.equalTo(headerView);
        make.width.offset(SCREEN_WIDTH).priorityHigh();
        make.bottom.equalTo(name.sales.mas_bottom).offset(8);
    }];
    [name layoutIfNeeded];
    //商店信息
    store=[[StoreInfoView alloc] init];
    [headerView addSubview:store];
    [store mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom).with.offset(10);
        make.left.right.equalTo(headerView);
        //        make.height.offset(130);
        make.bottom.equalTo(store.enter.mas_bottom).offset(10);
        make.bottom.equalTo(headerView.mas_bottom).with.offset(-10);
    }];
    [store layoutIfNeeded];
    
    //添加webview前计算一次高度
    CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = headerView.bounds;
    frame.size.height = height;
    headerView.frame = frame;
    [self.myTableview setTableHeaderView:headerView];
    [headerView layoutIfNeeded];
    
    //商品详情
    self.webView=[[UIWebView alloc]init];
    [_webView loadHTMLString:@"<p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000e7c34206.jpg\" style=\"float:none;\" title=\"详情1.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000e7c4f016.jpg\" style=\"float:none;\" title=\"详情2.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000e7c61772.jpg\" style=\"float:none;\" title=\"详情3.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000e7c75e5f.jpg\" style=\"float:none;\" title=\"详情4.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000e7c89c72.jpg\" style=\"float:none;\" title=\"详情5.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000e9fef977.jpg\" style=\"float:none;\" title=\"详情6.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000ea015e9f.jpg\" style=\"float:none;\" title=\"详情7.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000ea027d33.jpg\" style=\"float:none;\" title=\"详情8.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000ea03c10a.jpg\" style=\"float:none;\" title=\"详情9.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000ea050ac7.jpg\" style=\"float:none;\" title=\"详情10.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000ea065669.jpg\" style=\"float:none;\" title=\"详情11.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000ea07b763.jpg\" style=\"float:none;\" title=\"详情12.jpg\"/></p><p><img src=\"http://yunjifreebuy.dlgtech.cn/Uploads/goods/2017/04-26/59000ea08db84.jpg\" style=\"float:none;\" title=\"详情13.jpg\"/></p><p><br/></p>" baseURL:nil];
    self.webView.delegate=self;
    self.webView.scrollView.delegate = self;
    self.webView.scrollView.backgroundColor=[UIColor groupTableViewBackgroundColor];
    self.webView.userInteractionEnabled=NO;
}
- (void)getFooterView{
    //底部背景
    UIView *footerView=[UIView new];
    footerView.backgroundColor=[UIColor whiteColor];
    [self.view addSubview:footerView];
    
    [footerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-self.tabBar_height);
        make.left.right.offset(0);
        make.height.offset(44);
    }];
    
    
    //立即购买
    UIButton *purchase=[UIButton buttonWithType:UIButtonTypeCustom];
    [purchase setTitle:Localized(@"立即购买") forState:UIControlStateNormal];
    purchase.backgroundColor=[UIColor colorWithHexString:@"#ff802a"];
    [footerView addSubview:purchase];
    purchase.titleLabel.font=[UIFont systemFontOfSize:15];
    [purchase mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.right.equalTo(footerView);
        make.width.offset(110);
    }];
    //加入购物车
    UIButton *addCart=[UIButton buttonWithType:UIButtonTypeCustom];
    [addCart setTitle:Localized(@"加入购物车") forState:UIControlStateNormal];
    addCart.backgroundColor=[UIColor colorWithHexString:@"#fed900"];
    [footerView addSubview:addCart];
    addCart.titleLabel.font=[UIFont systemFontOfSize:15];
    [addCart mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(footerView);
        make.right.equalTo(purchase.mas_left);
        make.width.offset(110);
    }];
    //店铺，客服，收藏
    UIView *back=[[UIView alloc] init];
    [footerView addSubview:back];
    back.backgroundColor=[UIColor whiteColor];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.bottom.equalTo(footerView);
        make.right.equalTo(addCart.mas_left);
    }];
    [back layoutIfNeeded];
    NSLog(@"%f",back.frame.size.width);
    CGFloat width=back.frame.size.width/3;
    NSArray *array=@[Localized(@"店铺"),Localized(@"客服"),Localized(@"收藏")];
    NSArray *image=@[@"店铺",@"客服",@"收藏"];
    for (int i=0; i<array.count; i++) {
        UIButton *but=[UIButton buttonWithType:UIButtonTypeCustom];
        [but setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [but setTitle:array[i] forState:UIControlStateNormal];
        but.titleLabel.font=[UIFont systemFontOfSize:10];
        [but setImage:[UIImage imageNamed:image[i]] forState:UIControlStateNormal];
        [back addSubview:but];
        [but mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(back);
            make.width.offset(width);
            make.left.equalTo(back).with.offset(width*i);
        }];
        [but layoutIfNeeded];
        but.contentHorizontalAlignment=UIControlContentHorizontalAlignmentCenter;
        [but layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleTop imageTitleSpace:0];
    }

    UIView *line=[[UIView alloc]init];
    [footerView addSubview:line];
    line.backgroundColor=[UIColor colorWithHexString:@"#ECEBEC"];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(0.5);
    }];
}
-(void)webViewDidFinishLoad:(UIWebView *)webView
{
    //webview加载完成
    [self setWebViewHtmlImageFitPhone];
}
- (void)setWebViewHtmlImageFitPhone
{
    //刷新之前headview最底下的约束，删除底部的约束
    [store mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(name.mas_bottom).with.offset(10);
        make.left.right.equalTo(headerView);
        //        make.height.offset(130);
        make.bottom.equalTo(store.enter.mas_bottom).offset(10);
    }];
    [store layoutIfNeeded];
    
    //图片大小自适应屏幕
    NSString *jsStr = [NSString stringWithFormat:@"var script = document.createElement('script');"
                       "script.type = 'text/javascript';"
                       "script.text = \"function ResizeImages() { "
                       "var myimg,oldwidth;"
                       "var maxwidth = '%f';" //自定义宽度
                       "for(i=0;i <document.images.length;i++){"
                       "myimg = document.images[i];"
                       "if(myimg.width > maxwidth){"
                       "oldwidth = myimg.width;"
                       "myimg.width = maxwidth;"
                       "}"
                       "}"
                       "}\";"
                       "document.getElementsByTagName('head')[0].appendChild(script);",SCREEN_WIDTH-15];
    
    [self.webView stringByEvaluatingJavaScriptFromString:jsStr];
    [self.webView stringByEvaluatingJavaScriptFromString:@"ResizeImages();"];
    height = [[_webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue];
    
    _headView = [[Orderheader alloc]init];
    [headerView addSubview:_headView];
    _headView.backgroundColor=[UIColor whiteColor];
    [_headView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(store.mas_bottom).with.offset(10);
        make.left.right.equalTo(headerView);
        make.height.offset(40);
    }];
    //    _headView.backgroundColor=[UIColor colorWithHexString:@"#f8fafd"];
    __weak typeof(self) weakSelf = self;
    _headView.itemClickAtIndex = ^(NSInteger index){
        [weakSelf adjustScrollView:index];
    };
    _headView.items = @[Localized(@"商品详情"),Localized(@"商品评价")];
    
    
    _scroll = [[UIScrollView alloc]init];
    _scroll.bounces = NO;
    _scroll.pagingEnabled = YES;
    _scroll.delegate = self;
    [headerView addSubview:_scroll];
    _scroll.backgroundColor=[UIColor groupTableViewBackgroundColor];
    _scroll.userInteractionEnabled=NO;
    _scroll.contentSize = CGSizeMake(0, height);
    //
    [_scroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_headView.mas_bottom).with.offset(0);
        make.left.right.equalTo(headerView);
        make.height.offset(height);
        //        make.bottom.equalTo(headerView.mas_bottom).with.offset(-10);
    }];
    [_scroll.superview layoutIfNeeded];
    
    [_scroll addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scroll);
        make.left.equalTo(_scroll);
        make.height.offset(height);
        make.width.offset(SCREEN_WIDTH);
    }];
    [evaluate layoutIfNeeded];
    
    //评论
    evaluate=[[EvaluateView alloc] init];
    [_scroll addSubview:evaluate];
    [evaluate mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(_scroll);
        make.left.equalTo(_scroll).with.offset(SCREEN_WIDTH);
        make.width.offset(SCREEN_WIDTH);
        make.bottom.equalTo(evaluate.text.mas_bottom).with.offset(10);
        
        //        make.height.offset(height);
    }];
    [evaluate layoutIfNeeded];
    
    
    GoodsQRCode *code=[[GoodsQRCode alloc]init];
    [headerView addSubview:code];
    //    code.backgroundColor=[UIColor redColor];
    [code mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_scroll);
        make.top.equalTo(self.scroll.mas_bottom).offset(10);
        make.height.offset(264);
        
    }];
    
    RecommendView *recommend=[[RecommendView alloc]init];
    //    recommend.listArr=self.onsale;
    [headerView addSubview:recommend];
    [recommend mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(_scroll);
        if (iPhone5SE) {
            make.height.offset(240+40);
        }else  if (iPhone6_6s) {
            make.height.offset(262+40);
        }else  if (iPhone6Plus_6sPlus) {
            make.height.offset(282+40);
        }else{
            make.height.offset(262+40);
        }
        make.top.equalTo(code.mas_bottom).with.offset(0);
        make.bottom.equalTo(headerView.mas_bottom).with.offset(-10);
    }];
    [recommend layoutIfNeeded];
    
    
    //全部控件添加完成后重新计算一遍高度
    CGFloat headerHeight = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    
    CGRect frame = headerView.bounds;
    frame.size.height = headerHeight;
    headerView.frame = frame;
    [self.myTableview setTableHeaderView:headerView];
    //    [headerView layoutIfNeeded];
}
#pragma mark-通过点击button来改变scrollview的偏移量
-(void)adjustScrollView:(NSInteger)index
{
    if (index==0) {
        [_scroll mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_headView.mas_bottom).with.offset(0);
            make.left.right.equalTo(headerView);
            make.height.mas_equalTo(height);
        }];
        _scroll.contentSize = CGSizeMake(0, height);
    }else{
        [_scroll mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(_headView.mas_bottom).with.offset(0);
            make.left.right.equalTo(headerView);
            make.height.mas_equalTo(evaluate.frame.size.height);
        }];
        _scroll.contentSize = CGSizeMake(0, evaluate.frame.size.height);
    }
    [_scroll layoutIfNeeded];
    CGFloat height = [headerView systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    CGRect frame = headerView.bounds;
    frame.size.height = height;
    headerView.frame = frame;
    [self.myTableview setTableHeaderView:headerView];
    [headerView layoutIfNeeded];
    [UIView animateWithDuration:0.2 animations:^{
        _scroll.contentOffset = CGPointMake(index*_scroll.bounds.size.width, 0);
    }];
}

#pragma mark - 数据转model
- (void)analysisDataWithCycleDictionary:(NSDictionary*)dictionary{
    
    for (NSDictionary *dict in dictionary)
    {
        self.shopInfo = [ShopInfoModel yy_modelWithDictionary:dict];
        NSMutableArray *datas = [NSMutableArray array];
        for (cycleModel *sModel in self.shopInfo.cycle)
        {
            [datas addObject:sModel];
        }
        self.cycleArry =datas;
    }
}
#pragma mark - tableview代理
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 0;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 1;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    //总高度
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return nil;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return nil;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *ID = @"newCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;;
    }
    cell.opaque=YES;
    cell.layer.drawsAsynchronously=YES;
    cell.layer.rasterizationScale=[UIScreen mainScreen].scale;
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if (scrollView.contentOffset.y>=0 ) {
        if (scrollView.contentOffset.y<=(STATUS_BAR_HEIGHT+44)) {
            self.navBarBgAlpha=alpha=[NSString stringWithFormat:@"%f",scrollView.contentOffset.y/(STATUS_BAR_HEIGHT+44)];
        }else{
            self.navBarBgAlpha=alpha=@"1.0";
        }
        
    }else{
        
        self.navBarBgAlpha=alpha=@"0.0";
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (UITableView *)myTableview{
    if (!_myTableview) {
        _myTableview = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _myTableview.delegate = self;
        _myTableview.dataSource = self;
        _myTableview.backgroundColor=[UIColor groupTableViewBackgroundColor];
        _myTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
        _myTableview.showsVerticalScrollIndicator=NO;
        _myTableview.scrollEnabled = YES;
    }
    return _myTableview;
}
-(NSMutableArray *)cycleArry{
    if (!_cycleArry) {
        _cycleArry=[[NSMutableArray alloc] init];
    }
    return _cycleArry;
}
/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end

